/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlayerInitPanel.java
 *
 * Created on Jul 29, 2011, 1:51:33 PM
 */
package deatax.display.gui;

import deatax.network.ChangedListener;
import deatax.network.Client;
import deatax.network.Messages;
import deatax.network.NetworkListener;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JOptionPane;

/**
 *
 * @author pson
 */
public class JoinInitPanel extends javax.swing.JPanel implements NetworkListener, ChangedListener {

    private int n;

    /** Creates new form PlayerInitPanel */
    public JoinInitPanel() {
        initComponents();
    }

    public void open() {
        pnlPlayers.removeAll();
        Client.get().addListener(this);
    }

    public void close() {
        btnConnect.setEnabled(true);
        Client.get().removeListener(this);
    }

    public int getN() {
        System.out.println("returning n");
        return n;
    }

    public void assignPlayers() {
        for (Component c : pnlPlayers.getComponents()) {
            if (c instanceof PlayerCreatePanel) {
                PlayerCreatePanel pcp = (PlayerCreatePanel) c;
                Client.get().getControl().addPlayer(pcp.getPlayerName(), pcp.getColor());
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnConnect = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        pnlPlayers = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        portField = new deatax.display.gui.JLabeledTextField();
        ipField = new deatax.display.gui.JLabeledTextField();

        btnConnect.setText("Connect");
        btnConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConnectActionPerformed(evt);
            }
        });

        pnlPlayers.setLayout(new javax.swing.BoxLayout(pnlPlayers, javax.swing.BoxLayout.Y_AXIS));
        jScrollPane1.setViewportView(pnlPlayers);

        jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, jLabel1.getFont().getSize()+9));
        jLabel1.setText("Join Game");

        portField.setText("9999");
        portField.setEmptyText("Port");

        ipField.setText("localhost");
        ipField.setEmptyText("IP Address");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(portField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 129, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnConnect, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE))
                    .add(ipField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(ipField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnConnect)
                    .add(portField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConnectActionPerformed
        try {
            if (Client.get().connect(ipField.getText(), Integer.parseInt(portField.getText()))) {
                btnConnect.setEnabled(false);
            } else {
                JOptionPane.showMessageDialog(null, "Unable to connect to server.", "Connection Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            
        }
        validate();
    }//GEN-LAST:event_btnConnectActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConnect;
    private deatax.display.gui.JLabeledTextField ipField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnlPlayers;
    private deatax.display.gui.JLabeledTextField portField;
    // End of variables declaration//GEN-END:variables

    @Override
    public void processMessage(int type, String[] arg) {
        switch (type) {
            case Messages.CONNECT_DETAILS: {
                //number of self
                int pn = Integer.parseInt(arg[0]);
                //number of other players
                int nc = Integer.parseInt(arg[1]);
                for (int i = 0; i < nc; ++i) {
                    PlayerCreatePanel pcp = new PlayerCreatePanel(i+1);
                    pcp.setEditable(false);
                    pnlPlayers.add(pcp);
                }
                validate();
                n = pn;
            }
            break;
            case Messages.CLIENT_NAME: {
                int pn = Integer.parseInt(arg[0]);
                String nc = arg[1];
                int pc = Integer.parseInt(arg[2]);
                PlayerCreatePanel pcp = (PlayerCreatePanel)pnlPlayers.getComponent(pn - 1);
                if (!pcp.getPlayerName().equals(nc)) {
                    pcp.setPlayerName(nc);
                }
                pcp.setColour(new Color(pc));
                validate();
            }
            break;
            case Messages.NEW_CLIENT: {
                int pn = Integer.parseInt(arg[0]);
                PlayerCreatePanel pcp = new PlayerCreatePanel(pn);
                if (pn != n) {
                    pcp.setEditable(false);
                } else {
                    pcp.addListener(this);
                }
                pnlPlayers.add(pcp);
                validate();
            }
            break;
            default:
                break;
        }
    }
    
    @Override
    public void changed(String name, Color color) {
        System.out.println("change: " + name + " and " + color.toString());
        Client.get().sendMessage(Messages.CLIENT_NAME, new String[]{String.valueOf(n), name, String.valueOf(color.getRGB())});
    }
}
